import sys
from PyQt5.QtWidgets import *
 
class QLineEditMask(QWidget):
    def __init__(self):
        super(QLineEditMask, self).__init__()
        self.initUI()
    def initUI(self):
        self.setWindowTitle('用掩码限制QLineEdit控件的输入')
        formLayout=QFormLayout()
 
        ipLineEdit=QLineEdit() #ip地址
        macLineEdit=QLineEdit() #MAC地址
        dateLineEdit=QLineEdit() #日期
        licenseLineEdit=QLineEdit() #验证码
 
        #eg:192.168.21.45
        #最后加个分号和_意为没输入是默认显示_ ，前面的都是掩码，即ASCII数字字符是允许输入的，但不是必需的(0-9)
        ipLineEdit.setInputMask('000.000.000.000;_')
        macLineEdit.setInputMask('HH:HH:HH:HH:HH:HH;_')
        dateLineEdit.setInputMask('0000-00-00')
        licenseLineEdit.setInputMask('>AAAAA-AAAAA-AAAAA-AAAAA-AAAAA;#')#若没有输入显示井号
 
        formLayout.addRow('数字掩码',ipLineEdit)
        formLayout.addRow('MAC掩码',macLineEdit)
        formLayout.addRow('日期掩码',dateLineEdit)
        formLayout.addRow('许可证掩码',licenseLineEdit)
 
        self.setLayout(formLayout)
        return
 
 
if __name__=='__main__':
    app=QApplication(sys.argv)
    main=QLineEditMask()
    main.show()
    sys.exit(app.exec_())
 
 
